package com.moto.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.moto.bean.PostsFriendBean;
import com.moto.common.PostType;
import com.moto.entity.MotoUser;
import com.moto.entity.Posts;
import com.moto.entity.PostsFriend;
import com.moto.repository.MotoUserRepository;
import com.moto.repository.PostsFriendApplyRepository;
import com.moto.repository.PostsFriendRepository;
import com.moto.repository.PostsRepository;
import com.moto.service.PostsFriendService;
/**
 * 组队贴
 * @author lixunhui
 *
 */
@Service("postsFriendService")
public class PostsFriendServiceImpl implements PostsFriendService {
	
	
	private static final Logger LOG = Logger.getLogger(PostsFriendServiceImpl.class);
	
	@Autowired
	private PostsRepository postsRepository;
	@Autowired
	private MotoUserRepository motoUserRepository;
	@Autowired
	private PostsFriendRepository postsFriendRepository;
	@Autowired
	private PostsFriendApplyRepository postsFriendApplyRepository;
	
	@Override
	public int deleteByPrimaryKey(String pId) {
		return postsFriendRepository.deleteByPrimaryKey(pId);
	}

	@Override
	public int insert(PostsFriend record) {
		LOG.info("插入组队贴："+record.toString());
		return postsFriendRepository.insert(record);
	}

	@Override
	public int insertSelective(PostsFriend record) {
		LOG.info("插入组队贴："+record.toString());
		return postsFriendRepository.insertSelective(record);
	}

	@Override
	public PostsFriend selectByPrimaryKey(String pId) {
		return postsFriendRepository.selectByPostsId(pId);
	}

	@Override
	public int updateByPrimaryKeySelective(PostsFriend record) {
		return postsFriendRepository.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(PostsFriend record) {
		return postsFriendRepository.updateByPrimaryKey(record);
	}
	
	@Override
	public List<PostsFriendBean> getAll(String postsArea) {
		LOG.info("分页查询组队贴信息");
		//第一步 查询帖子信息，去POSTS表中查询
		
		List<Posts> postsList = postsRepository.getAllByType(PostType.POST_TYPE_FRIEND);
		
		//第二步，根据ID去POSTS_FRIEND表中查询，去顶lack
		List<PostsFriendBean>  list = copyPostsFriendBean(postsList);
		
		Page<Posts> page = (Page<Posts>) postsList;
		
		Page<PostsFriendBean> beanpage = new Page<PostsFriendBean>();
		beanpage.setCount(page.isCount());
		beanpage.setPageNum(page.getPageNum());
		beanpage.setPageSize(page.getPageSize());
		beanpage.setStartRow(page.getStartRow());
		beanpage.setEndRow(page.getEndRow());
		beanpage.setTotal(page.getTotal());
		beanpage.setPages(page.getPages());
		beanpage.setCountSignal(page.getCountSignal());
		beanpage.setOrderBy(page.getOrderBy());
		beanpage.setOrderByOnly(page.isOrderByOnly());
		beanpage.setReasonable(page.getReasonable());
		beanpage.setPageSizeZero(page.getPageSizeZero());
		beanpage.addAll(list);
		
		Collections.sort(list, new Comparator<PostsFriendBean>(){
			@Override
			public int compare(PostsFriendBean o1, PostsFriendBean o2) {
				String postsUpdatetime = o1.getPostsUpdateTime();
				String postsUpdatetime2 = o2.getPostsUpdateTime();
				
				Long t1 = Long.valueOf(postsUpdatetime);
				Long t2 = Long.valueOf(postsUpdatetime2);
				if(t1>t2){
					return -1;
				}
				if(t1==t2){
					return 0;
				}
				return 1;
			}
		});
		
		return beanpage;
	}

/**
 * 关联查询
 * @param postsList
 * @return
 */
	private List<PostsFriendBean> copyPostsFriendBean(List<Posts> postsList) {
		List<PostsFriendBean> lists = Lists.newArrayListWithCapacity(postsList.size());
		for (Posts posts : postsList) {
			PostsFriend postsFriend = postsFriendRepository.selectByPostsId(posts.getPostsId());
			PostsFriendBean bean = new PostsFriendBean();
			bean.setPostsArea(posts.getPostsArea());
			bean.setPostsAssembleTime(postsFriend.getPostsAssembleTime());
			bean.setPostsComment(posts.getPostsComment());
			bean.setPostsContent(posts.getPostsContent());
			bean.setPostsCreatetime(posts.getPostsCreatetime());
			bean.setPostsDestination(postsFriend.getPostsDestination());
			bean.setPostsId(postsFriend.getPostsId());
			bean.setPostsLike(posts.getPostsLike());
			bean.setPostsPhone(postsFriend.getPostsPhone());
			bean.setPostsPics(posts.getPostsPics());
			bean.setPostsPicsSize(posts.getPostsPicsSize());
			bean.setPostsPicsDesc(posts.getPostsPicsDesc());
			bean.setPostsPopulation(postsFriend.getPostsPopulation());
			bean.setPostsRead(posts.getPostsRead());
			bean.setPostsRendezvous(postsFriend.getPostsRendezvous());
			bean.setPostsSubject(posts.getPostsSubject());
			bean.setPostsType(posts.getPostsType());
			bean.setPostsUpdateTime(posts.getPostsUpdateTime());
			MotoUser motoUser = motoUserRepository.selectByPrimaryKey(posts.getPostUserId());
			bean.setPostsUser(motoUser);
			int applyCount = postsFriendApplyRepository.selectApplyCount(posts.getPostsId());
			Long count = postsFriend.getPostsPopulation();
			
			if(0==applyCount){
				bean.setLack(count);
			}else{
				Long applyCountLong  = (long) applyCount;
				Long lack = count-applyCountLong;
				bean.setLack(lack);
			}
			lists.add(bean);
		}
		return lists;
	}

	public PostsFriendBean copyBean(PostsFriend postsFriend){
		Posts posts = postsRepository.selectByPrimaryKey(postsFriend.getPostsId());
		PostsFriendBean bean = new PostsFriendBean();
		bean.setPostsArea(posts.getPostsArea());
		bean.setPostsAssembleTime(postsFriend.getPostsAssembleTime());
		bean.setPostsComment(posts.getPostsComment());
		bean.setPostsContent(posts.getPostsContent());
		bean.setPostsCreatetime(posts.getPostsCreatetime());
		bean.setPostsDestination(postsFriend.getPostsDestination());
		bean.setPostsId(postsFriend.getPostsId());
		bean.setPostsLike(posts.getPostsLike());
		bean.setPostsPhone(postsFriend.getPostsPhone());
		bean.setPostsPics(posts.getPostsPics());
		bean.setPostsPicsSize(posts.getPostsPicsSize());
		bean.setPostsPicsDesc(posts.getPostsPicsDesc());
		bean.setPostsPopulation(postsFriend.getPostsPopulation());
		bean.setPostsRead(posts.getPostsRead());
		bean.setPostsRendezvous(postsFriend.getPostsRendezvous());
		bean.setPostsSubject(posts.getPostsSubject());
		bean.setPostsType(posts.getPostsType());
		bean.setPostsUpdateTime(posts.getPostsUpdateTime());
		MotoUser motoUser = motoUserRepository.selectByPrimaryKey(posts.getPostUserId());
		bean.setPostsUser(motoUser);
		int applyCount = postsFriendApplyRepository.selectApplyCount(posts.getPostsId());
		Long count = postsFriend.getPostsPopulation();
		
		if(0==applyCount){
			bean.setLack(count);
		}else{
			Long applyCountLong  = (long) applyCount;
			Long lack = count-applyCountLong;
			bean.setLack(lack);
		}
		return bean;
	}

	@Override
	public int deleteByPostsId(String postsId) {
		return postsFriendRepository.deleteByPostsId(postsId);
	}

	@Override
	public PostsFriend selectByPostsId(String postsId) {
		LOG.info("根据帖子ID查询贴子信息");
		return postsFriendRepository.selectByPostsId(postsId);
	}
}
